package pers.corvey.studyjava.model.form;

import java.util.Objects;

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import pers.corvey.studyjava.controller.CaptchaController;
import pers.corvey.studyjava.model.User;
import pers.corvey.studyjava.util.ToStringUtils;

public class RegisterForm implements MyValidatable {

    @NotNull(message="账号不能为空")
    @Size(min=User.ACCOUNT_MIN_LEN, max=User.ACCOUNT_MAX_LEN, message="账号长度有误")
    private String account;
    
    @NotNull(message="密码不能为空")
    @Size(min=User.PASSWORD_MIN_LEN, max=User.PASSWORD_MAX_LEN, message="密码长度有误")
    private String password;
    
    @NotNull(message="重复密码不能为空")
    @Size(min=User.PASSWORD_MIN_LEN, max=User.PASSWORD_MAX_LEN, message="重复密码长度有误")
    private String repassword;
    
    @NotNull(message="昵称不能为空")
    @Size(min=User.NAME_MIN_LEN, max=User.NAME_MAX_LEN, message="昵称长度有误")
    private String name;
    
    @NotNull(message="性别不能为空")
    @Pattern(regexp="^[男女]$", message="性别必须为男或女")
    private String gender;
    
    @NotNull(message="验证码不能为空")
    @Size(min=CaptchaController.CAPTCHA_LENGTH, max=CaptchaController.CAPTCHA_LENGTH, message="验证码长度有误")
    private String captcha;
    
    @AssertTrue(message="两次输入的密码不相同")
    private boolean isPasswordEqual;
    
    public RegisterForm() {}
    
    public User toUser() {
        User user = new User();
        user.setAccount(getAccount());
        user.setPassword(getPassword());
        user.setName(getName());
        user.setGender(getGender());
        return user;
    }
    
    @Override
    public void validate() {
        isPasswordEqual = Objects.equals(getPassword(), getRepassword());
        MyValidatable.super.validate();
    }
    
    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRepassword() {
        return repassword;
    }

    public void setRepassword(String repassword) {
        this.repassword = repassword;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getCaptcha() {
        return captcha;
    }

    public void setCaptcha(String captcha) {
        this.captcha = captcha;
    }
    
    @Override
    public String toString() {
        return ToStringUtils.objectToString(this);
    }
}
